package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SelectUserFlagAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
@AttrHandler(SelectUserFlagAttrHandler.class)
public class SelectUserFlagAttrLinkHandler  extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    IOperateSceneInstService operateSceneInstService;
    @Autowired
    private OperateInstAttrService operateInstAttrService;
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String value = attr.getValue();
                String attrVal = attr.getPreValue();
        if(KiteStringUtils.isNotEmpty(value)&&KiteStringUtils.isNotEmpty(attrVal)) {
            String[] newVal = value.split(",");
            if (value.contains("110008292") && newVal.length > 1) {
                //BssException.throwOut(KeyConsts.LOG_MODULE, "","不支持选择组合");
                value = Arrays.stream(newVal).filter(nv -> !attrVal.contains(nv)).findFirst().get();
            }
        }
        String dataJson = JSONObject.toJSONString(data);
        operateInstAttrService.setAttrValue(inst, attr.getFieldName(), value, data.getSceneInstId(), dataJson);
        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
